//
//  RSTabBarController.m
//  RSTabBar
//
//  Created by thinkjoy on 2017/8/4.
//  Copyright © 2017年 杜瑞胜. All rights reserved.
//

#import "RSTabBarController.h"
#import "RSTabBar.h"

#import "TJZF_HomePageVC.h"
#import "TJZF_MessageQueryVC.h"
#import "TJZF_MoveZFVC.h"
#import "TJZF_MoveOAVC.h"

#import "TweetEditingVC.h"

@interface RSTabBarController ()<UITabBarControllerDelegate>

@property   (nonatomic,assign)  CGFloat  KWinW;

@property   (nonatomic,strong)  UIColor * normalColor;
@property   (nonatomic,strong)  UIColor * selectColor;


@property (nonatomic, strong) UIButton *centerButton;

@end

@implementation RSTabBarController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //隐藏导航栏
    [self.navigationController.navigationBar setHidden:YES];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [self.navigationController.navigationBar setHidden:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _KWinW  =   [[UIScreen mainScreen] bounds].size.width;
    _normalColor    =   [UIColor lightGrayColor];
    _selectColor    =   [UIColor blueColor];
    
    // 初始化所有控制器
    [self setUpChildVC];
    
    [self addCenterButtV];
    
}

- (void)setUpChildVC{
    
    TJZF_HomePageVC *   homePageVC  =   [TJZF_HomePageVC new];
    TJZF_MessageQueryVC * messageQueryVC    =   [TJZF_MessageQueryVC new];
    TJZF_MoveZFVC   *   moveZFVC    =   [TJZF_MoveZFVC new];
    TJZF_MoveOAVC   *   moveOAVC    =   [TJZF_MoveOAVC new];
    moveOAVC.tabBarItem.badgeValue = @"99";
    
    [self   addOneChildVC:homePageVC title:@"首页" imageName:@"首页" selectedImageName:@"首页-选中"];
    [self   addOneChildVC:messageQueryVC title:@"信息查询" imageName:@"信息查询" selectedImageName:@"信息查询-选中"];
    [self   addOneChildVC:moveZFVC title:@"移动执法" imageName:@"移动执法" selectedImageName:@"移动执法-选中"];
    [self   addOneChildVC:moveOAVC title:@"移动OA" imageName:@"移动OA" selectedImageName:@"移动OA-选中"];
    
}
- (void)addOneChildVC:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName {
    
    // 设置标题
    childVc.tabBarItem.title = title;
    [childVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:_normalColor} forState:UIControlStateNormal];
    [childVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:_selectColor} forState:UIControlStateSelected];
    childVc.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -4);
    // 设置图标
    childVc.tabBarItem.image    =   [UIImage imageNamed:imageName];
    
    //    // 不要渲染
    UIImage * selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVc.tabBarItem.selectedImage =  selectedImage;
    //    [UIImage imageNamed:selectedImageName];
    
    // 添加为tabbar控制器的子控制器
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:childVc];
    [nav.navigationBar setShadowImage:[UIImage new]];
    
    nav.navigationBar.barTintColor    =   [UIColor lightGrayColor];
    [nav.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor redColor]}];
    
    [self addChildViewController:nav];
}

#pragma mark    - 添加中心按钮
- (void)addCenterButtV
{
    RSTabBar *tabBar = [[RSTabBar alloc] init];
    [self setValue:tabBar forKey:@"tabBar"];
    
    __weak typeof(self) weakSelf = self;
    [tabBar setClickCenterButtBlock:^{
        [weakSelf popCenterVC];
    }];
}

- (void)popCenterVC
{
    TweetEditingVC *tweetEditingVC = [TweetEditingVC new];
    UINavigationController *tweetEditingNav = [[UINavigationController alloc] initWithRootViewController:tweetEditingVC];
    [self.selectedViewController presentViewController:tweetEditingNav animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    
    NSInteger index = [self.tabBar.items indexOfObject:item];
    NSLog(@"点击了%d",(int)index);
    [self itemAnimationWithIndex:index];
}
- (void)itemAnimationWithIndex:(NSInteger)index
{
    NSMutableArray *muAry = [NSMutableArray array];

    Class class = NSClassFromString(@"UITabBarButton");
    for (UIView *btn in self.tabBar.subviews) {//遍历tabbar的子控件
        if ([btn isKindOfClass:class]) {//如果是系统的UITabBarButton，那么就
            [muAry addObject:btn];
        }
    }
    
    CABasicAnimation *animaiton = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    animaiton.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animaiton.duration = .2;
    animaiton.repeatCount = 1;
    animaiton.autoreverses  =   YES;
    animaiton.fromValue =   @0.8;
    animaiton.toValue = @1.3;
    [[muAry[index] layer] addAnimation:animaiton forKey:nil];

}


@end
